package deep.projects.newsapp.activities;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.widget.Button;
import android.widget.EditText;

import com.wuxinwudai.android.util.RegexUtils;
import com.wuxinwudai.android.util.ToastUtils;
import com.wuxinwudai.android.util.ViewUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.https.HttpsUtils;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import deep.projects.newsapp.R;
import deep.projects.newsapp.app.Urls;
import okhttp3.Call;
import okhttp3.Response;

/**
 * RegisterActivity 为注册页面
 * @author 吾心无待 于2016年03月05日
 */
public class RegisterActivity extends BaseLayoutActivity {
    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }
    @BindView(R.id.et_mobile) EditText metMobile;//手机号码
    @BindView(R.id.et_smscode) EditText metCode;//验证码
    @BindView(R.id.et_password) EditText metPassword;//密码
    @BindView(R.id.et_confirm_password) EditText metConfirmPwd;//密码确认
    @BindView(R.id.btn_send_sms_code) Button mbtnSendSmsCode;//发送短信验证码
    //阅读注册协议
    @OnClick(R.id.tv_clause) void onReadClause(){
        ViewUtils.goWebViewActivity(RegisterActivity.this, WebSettings.LOAD_DEFAULT,"file:///android_asset/html/clause.html","新闻APP用户协议");
    }
    //注册
    @OnClick(R.id.btn_register) void onRegister(View v){
        String mobile = metMobile.getText().toString();
        if(!RegexUtils.isMobile(mobile)){
            ToastUtils.showShort(this,R.string.msg_mobile_format_incorrect);
            return;
        }
    }
    //发送短信验证码
    @OnClick(R.id.btn_send_sms_code) void onSendSmsCode(View v){
        String mobile = metMobile.getText().toString();
        String code = metCode.getText().toString();
        String pwd = metPassword.getText().toString();
        if(!RegexUtils.isMobile(mobile)){
            ToastUtils.showShort(this,R.string.msg_mobile_format_incorrect);
        }else{
            OkHttpUtils.post().url(Urls.POST_REGISTER_SMS_CODE)
                    .addParams("mobile",mobile)
                    .addParams("code",code)
                    .build()
                    .execute(new Callback<JSONObject>(){
                @Override
                public JSONObject parseNetworkResponse(Response response) throws Exception {
                    return new JSONObject(response.body().string());
                }

                @Override
                public void onError(Call call, Exception e) {
                    ToastUtils.showShort(RegisterActivity.this,"短信验证码发送失败，请重新发送！");
                }

                @Override
                public void onResponse(JSONObject response) {
                    try{
                        if (response.getBoolean("ok")){
                            ToastUtils.showShort(RegisterActivity.this,R.string.msg_send_mode_success);
                        }else{
                            ToastUtils.showShort(RegisterActivity.this,response.getString("message"));
                        }
                    }catch (Exception ex){
                        ToastUtils.showShort(RegisterActivity.this,R.string.msg_post_error);
                    }

                }
            });
        }
    }
    //查看注册协议

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        tvTitle.setText(R.string.title_activity_register);
    }

}
